package br.com.usjt.tcc.schedule.jpa.dao;

import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import br.com.usjt.tcc.schedule.interfaces.dao.NGODao;
import br.com.usjt.tcc.schedule.model.NGO;

@RequestScoped
public class JpaNGODao implements NGODao {

	@Inject
	private EntityManager entityManager;

	public void adiciona(NGO ngo) {
		entityManager.persist(ngo.getAdministrator().getAddress());
		entityManager.persist(ngo.getAdministrator());
		entityManager.persist(ngo);
	}

	public void atualiza(NGO ngo) {
		entityManager.merge(ngo);
	}

	@SuppressWarnings("unchecked")
	public List<NGO> lista() {
		Query query = entityManager.createQuery("select n from NGO n");
		return query.getResultList();
	}

	public NGO busca(Long id) {
		return entityManager.find(NGO.class, id);
	}

	public NGO buscaPeloDocument(String cnpj) {
		TypedQuery<NGO> query = entityManager.createQuery(
				"select n from NGO n where n.cnpj=:pCnpj", NGO.class);

		query.setParameter("pCnpj", cnpj);
		NGO ngo = query.getSingleResult();
		return ngo;
	}

	public NGO buscaPeloADM(Long id) {
		TypedQuery<NGO> query = entityManager.createQuery(
				"select n from NGO n where n.administrator.id=:pId", NGO.class);

		query.setParameter("pId", id);
		NGO ngo = query.getSingleResult();
		return ngo;
	}
}
